package com.hckjsec.cloud.service.impl;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import com.hckjsec.cloud.domain.AppletsConfigParam;
import com.hckjsec.cloud.domain.ConfigBasicParam;
import com.hckjsec.cloud.enums.BasicParamEnum;
import com.hckjsec.cloud.mapper.AppletsConfigParamMapper;
import com.hckjsec.cloud.service.IAppletsConfigParamService;
import com.hckjsec.common.utils.ComUtil;
import com.hckjsec.common.utils.EnumUtil;
import com.hckjsec.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 配置参数Service业务层处理
 * 
 * @author cyt
 * @date 2021-07-29
 */
@Service
public class AppletsConfigParamServiceImpl implements IAppletsConfigParamService
{
    @Autowired
    private AppletsConfigParamMapper appletsConfigParamMapper;

    /**
     * 查询配置参数
     * 
     * @param id 配置参数ID
     * @return 配置参数
     */
    @Override
    public AppletsConfigParam selectAppletsConfigParamById(Long id)
    {
        return appletsConfigParamMapper.selectAppletsConfigParamById(id);
    }

    /**
     * 查询配置参数列表
     * 
     * @param appletsConfigParam 配置参数
     * @return 配置参数
     */
    @Override
    public List<AppletsConfigParam> selectAppletsConfigParamList(AppletsConfigParam appletsConfigParam)
    {
        return appletsConfigParamMapper.selectAppletsConfigParamList(appletsConfigParam);
    }

    /**
     * 新增配置参数
     * 
     * @param appletsConfigParam 配置参数
     * @return 结果
     */
    @Override
    public int insertAppletsConfigParam(AppletsConfigParam appletsConfigParam)
    {
        return appletsConfigParamMapper.insertAppletsConfigParam(appletsConfigParam);
    }

    /**
     * 修改配置参数
     * 
     * @param appletsConfigParam 配置参数
     * @return 结果
     */
    @Override
    public int updateAppletsConfigParam(AppletsConfigParam appletsConfigParam)
    {
        return appletsConfigParamMapper.updateAppletsConfigParam(appletsConfigParam);
    }

    /**
     * 批量删除配置参数
     * 
     * @param ids 需要删除的配置参数ID
     * @return 结果
     */
    @Override
    public int deleteAppletsConfigParamByIds(Long[] ids)
    {
        return appletsConfigParamMapper.deleteAppletsConfigParamByIds(ids);
    }

    /**
     * 删除配置参数信息
     * 
     * @param id 配置参数ID
     * @return 结果
     */
    @Override
    public int deleteAppletsConfigParamById(Long id)
    {
        return appletsConfigParamMapper.deleteAppletsConfigParamById(id);
    }

    @Override
    public List<Map<String, String>> findMap(Long configId) {
        return appletsConfigParamMapper.findMap(configId);
    }

    @Override
    public int updateAppletsConfigParamByConfigId(AppletsConfigParam appletsConfigParam) {
        return appletsConfigParamMapper.updateAppletsConfigParamByConfigId(appletsConfigParam);
    }
    /**
     * 获取参数信息
     *
     * @param id 配置参数ID
     * @return 结果
     */
    public List<ConfigBasicParam> configBasicParamList(Long id){
        List<Map<String,String>> stateMap= appletsConfigParamMapper.findMap(id);
        Map<String,String> resState = ComUtil.mapListByMap(stateMap,"paramnumber");
        List<ConfigBasicParam> configList= new ArrayList<ConfigBasicParam>();
        List<Hashtable<String, String>> lists = EnumUtil.enum2List(BasicParamEnum.class);
        for (Hashtable<String, String> hashtable : lists) {
            ConfigBasicParam configBasicParam = new ConfigBasicParam();
            configBasicParam.setConfigname(hashtable.get("key"));
            configBasicParam.setConfigitems(hashtable.get("value"));
            configBasicParam.setConfiguration(hashtable.get("isbtn"));
            configBasicParam.setEnable(hashtable.get("ischecked"));
            if(resState.get(hashtable.get("key")) == null){
                AppletsConfigParam appletsConfigParam = new AppletsConfigParam();
                appletsConfigParam.setConfigId(id);
                appletsConfigParam.setParamnumber(hashtable.get("key"));
                appletsConfigParam.setParamname(hashtable.get("value"));
                appletsConfigParam.setState(hashtable.get("ischecked"));
                appletsConfigParam.setOperator(SecurityUtils.getUserId());
                appletsConfigParamMapper.insertAppletsConfigParam(appletsConfigParam);
            }else{
                configBasicParam.setEnable(resState.get(hashtable.get("key")));
            }
            configList.add(configBasicParam);
        }
        return configList;
    }
}
